package org.linlinjava.litemall.db.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.LitemallUserMapper;
import org.linlinjava.litemall.db.dao.LitemallUserMemberMapper;
import org.linlinjava.litemall.db.dao.UserMemberFreezeMapper;
import org.linlinjava.litemall.db.domain.LitemallUserMember;
import org.linlinjava.litemall.db.domain.UserMemberFreeze;
import org.linlinjava.litemall.db.exception.ServiceException;
import org.linlinjava.litemall.db.service.UserMemberFreezeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;

@Service
public class UserMemberFreezeServiceImpl extends ServiceImpl<UserMemberFreezeMapper, UserMemberFreeze> implements UserMemberFreezeService {


    @Autowired
    private LitemallUserMemberMapper litemallUserMemberMapper;
    @Autowired
    private LitemallUserMapper litemallUserMapper;


    @Override
    public void insert(UserMemberFreeze userMemberFreeze) {


        // 获取会员信息
        LitemallUserMember userMember = litemallUserMemberMapper.selectByPrimaryKey(userMemberFreeze.getMemberId());
        if (ObjectUtil.isNull(userMember)){
            throw new ServiceException("会员不存在");
        }
        // 会员冻结
        UserMemberFreeze dataFreeze = baseMapper.selectOne(
                Wrappers. <UserMemberFreeze>lambdaQuery().eq(UserMemberFreeze::getMemberId, userMember.getId())
        );

        if (userMemberFreeze.getState().equals("0")) {

            LocalDateTime dateTime = LocalDateTime.now();

            LocalDateTime expirationTime = userMember.getExpirationTime();
            if (dateTime.isAfter(expirationTime)) {
                throw new ServiceException("会员已过期");
            }
            if (ObjectUtil.isNotNull(dataFreeze) && dataFreeze.getState().equals("0")) {
                throw new ServiceException("会员已冻结");
            }
            Duration duration = Duration.between(dateTime, expirationTime);
            Long timeStamp = duration.getSeconds();
            // 计算会员冻结时间
            userMemberFreeze.setCreateDate(new Date());
            userMemberFreeze.setFreezeDays(Integer.valueOf(timeStamp.toString()));
            userMemberFreeze.setState("0");
            userMemberFreeze.setUserId(userMember.getUserId());
            userMemberFreeze.setMemberId(userMember.getId());
            userMemberFreeze.setDeleteStatus("0");
            save(userMemberFreeze);
            userMember.setEffective("2");
            litemallUserMemberMapper.updateByPrimaryKey(userMember);
        } else {
            if (userMemberFreeze.getState().equals("1")) {
                if (ObjectUtil.isNotNull(dataFreeze) && dataFreeze.getState().equals("1")) {
                    throw new ServiceException("会员已解冻");
                }
                userMemberFreeze.setUserId(userMember.getUserId());
                userMember.setEffective("0");
                userMember.setExpirationTime(LocalDateTime.now().
                        plusSeconds(dataFreeze.getFreezeDays()));
                // 更新秒数
                dataFreeze.setState("1");
                updateById(dataFreeze);
                baseMapper.deleteById(dataFreeze);
                userMember.setEffective("1");
                litemallUserMemberMapper.updateByPrimaryKey(userMember);
            }
        }
    }
}
